package au.com.evoCoach.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import au.com.evoCoach.domain.Player;
import au.com.evoCoach.domain.TeamPlayer;

public class FrontEndUtil {
	public static String getDivContents(Map<String, Integer> playerPositions, List<TeamPlayer> players) {
		String contents = "";
		int count = 1;
		for (Iterator<String> iter = playerPositions.keySet().iterator(); iter.hasNext();) {
			String position = iter.next();
			if (playerPositions.get(position) != null) {
				TeamPlayer player = getPlayer(playerPositions.get(position), players);
				if (player != null) {
					String style = getStyleForPosition(position) + " z-index: 9" + (count <= 9?"0":"") + count;
					contents += "<div id=\"player" + count + "\" class=\"dragDiv\" style=\"" + style + "\">\n";
					contents += "<h5 class=\"handler\"><table width=\"100%\"><tr><td width=\"50%\">" + player.getPlayer().getPositions() + "</td><td width=\"50%\" align=\"right\"><img src=\"images/icons/" + player.getPlayer().getTeam().getShortLabel() + ".png\" height=\"25px\" width=\"25px\"></td></tr></table></h5>\n";
					contents += "<div class=\"content\" id=\"content" + count + "\">" + player.getPlayer().getFullName() + "</div>\n";
					contents += "<div id=\"idVal" + count + "\" style=\"display: none\">" + player.getId() + "</div>\n";
					contents += "<div id=\"possies" + count + "\" style=\"display: none\">" + player.getPlayer().getPositions() + "</div>\n";
					int start = style.indexOf("left") + 6;
					contents += "<div id=\"oldLeft" + count + "\" style=\"display: none\">" + style.substring(start, style.indexOf(";", start)) + "</div>\n";
					start = style.indexOf("top") + 5;
					contents += "<div id=\"oldTop" + count + "\" style=\"display: none\">" + style.substring(start, style.indexOf(";", start)) + "</div>\n";
					contents += "<div id=\"oldZIndex" + count + "\" style=\"display: none\">9" + (count <= 9?"0":"") + count + "</div>\n";
					contents += "<div id=\"oldPossie" + count + "\" style=\"display: none\">" + position + "</div>\n";
					contents += "</div>\n";
					count++;
				}
			}
		}
		
		return contents;
	}
	
	private static TeamPlayer getPlayer(Integer id, List<TeamPlayer> players) {
		for (TeamPlayer player : players) {
			if (player.getId() == id) {
				return player;
			}
		}
		
		return null;
	}
	
	public static String getDivContentsNonSelectedPlayers(List<TeamPlayer> nonSelectedPlayers) {
		String contents = "";
		int count = 19;
		int top = 20;
		for (TeamPlayer player : nonSelectedPlayers) {
			contents += "<div id=\"player" + count + "\" class=\"dragDiv\" style=\"top: " + top + "px; left: " + ((count%2 == 0)?"170px":"20px") + "; z-index: 8" + (count <= 9?"0":"") + count + "\">\n";
			contents += "<h5 class=\"handler\"><table width=\"100%\"><tr><td width=\"50%\">" + player.getPlayer().getPositions() + "</td><td width=\"50%\" align=\"right\"><img src=\"images/icons/" + player.getPlayer().getTeam().getShortLabel() + ".png\" height=\"25px\" width=\"25px\"></td></tr></table></h5>\n";
			contents += "<div class=\"content\" id=\"content" + count + "\">" + player.getPlayer().getFullName() + "</div>\n";
			contents += "<div id=\"idVal" + count + "\" style=\"display: none\">" + player.getId() + "</div>\n";
			contents += "<div id=\"possies" + count + "\" style=\"display: none\">" + player.getPlayer().getPositions() + "</div>\n";
			contents += "<div id=\"oldLeft" + count + "\" style=\"display: none\">" + ((count%2 == 0)?"170px":"20px") + "</div>\n";
			contents += "<div id=\"oldTop" + count + "\" style=\"display: none\">" + top + "px</div>\n";
			contents += "<div id=\"oldZIndex" + count + "\" style=\"display: none\">8" + (count <= 9?"0":"") + count + "</div>\n";
			contents += "<div id=\"oldPossie" + count + "\" style=\"display: none\">RES</div>\n";
			contents += "</div>\n";
			if (count%2 == 0) {
				top += 95;
			}

			count++;
		}
		
		return contents;
	}	
	
	private static String getStyleForPosition(String position) {
	    String style = "";
	    if (position.substring(0, 1).equals("D")) {
	        if (Integer.valueOf(position.substring(1, 2)) <= 3) {
	        	style = " top: 140px;";
	        }
	        else {
	        	style = " top: 250px;";
	        }
	    }
	    else if (position.substring(0, 1).equals("M")) {
	        if (Integer.valueOf(position.substring(1, 2)) <= 3) {
	        	style = "top: 370px;";
	        }
	        else {
	        	style = "top: 580px;";
	        }
	    }
	    else if (position.substring(0, 1).equals("F")) {
	        if (Integer.valueOf(position.substring(1, 2)) <= 3) {
	        	style = "top: 710px;";
	        }
	        else {
	        	style = "top: 820px;";
	        }
	    }
	    else {
	    	style = "top: 475px;";
	    }
	    
	    switch (Integer.valueOf(position.substring(1, 2))) {
	    	case 1:
	    		if (position.substring(0, 1).equals("R")) {
	    			style += " left: 642px;";
	    			break;
	    		}
	    	case 4:
	    		style += " left: 472px;";    		
	    		break;
	    	case 2:
	    	case 5:
	    		style += " left: 642px;";    		
	    		break;    		
	    	case 3:
	    	case 6:
	    		style += " left: 812px;";    		
	    		break;    		
	    }
	    
	    return style;
	}
}
